# import base64
#
# z = 'Mozilla,Netscape,5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36,[object NetworkInformation],true,,[object Geolocation],4,zh-CN,zh-CN,zh,en,0,[object MediaCapabilities],[object MediaSession],[object MimeTypeArray],true,[object Permissions],Win32,[object PluginArray],Gecko,20030107,[object UserActivation],Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36,Google Inc.,,[object DeprecatedStorageQuota],[object DeprecatedStorageQuota],1040,0,0,1920,24,1080,[object ScreenOrientation],24,1920,[object DOMStringList],function assign() { [native code] },,match.yuanrenxue.com,match.yuanrenxue.com,http://match.yuanrenxue.com/match/14,http://match.yuanrenxue.com,/match/14,,http:,function reload() { [native code] },function replace() { [native code] },,function toString() { [native code] },function valueOf() { [native code] }'
#
# res = base64.b64encode(z.encode())
#
# print(bytes.decode(res))
# print(str(res, encoding="utf8"))
#
# import execjs
#
# with open('../../resources/match14/14m_raw.js', 'r', encoding='utf8') as f:
#     js_ctx = execjs.compile('var window = global;' + f.read())
#
# v14 = js_ctx.eval('window["v14"]')
# print(v14)

import execjs
import requests as req

from codes.commons.configs import easiest_common_headers as common_headers


def get_mjs_exec():
    with open('../../resources/match14/m_break.js', 'r', encoding='utf8') as f:
        mjs = f.read()
    m_exec = execjs.compile(mjs)
    return m_exec


def request_process():
    # 浏览器指纹，可取样暂做为固定值
    mz = 'TW96aWxsYSxOZXRzY2FwZSw1LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzg3LjAuNDI4MC42NiBTYWZhcmkvNTM3LjM2LFtvYmplY3QgTmV0d29ya0luZm9ybWF0aW9uXSx0cnVlLCxbb2JqZWN0IEdlb2xvY2F0aW9uXSw4LGVzLUVTLGVzLUVTLGVzLDAsW29iamVjdCBNZWRpYUNhcGFiaWxpdGllc10sW29iamVjdCBNZWRpYVNlc3Npb25dLFtvYmplY3QgTWltZVR5cGVBcnJheV0sdHJ1ZSxbb2JqZWN0IFBlcm1pc3Npb25zXSxXaW4zMixbb2JqZWN0IFBsdWdpbkFycmF5XSxHZWNrbywyMDAzMDEwNyxbb2JqZWN0IFVzZXJBY3RpdmF0aW9uXSxNb3ppbGxhLzUuMCAoV2luZG93cyBOVCAxMC4wOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvODcuMC40MjgwLjY2IFNhZmFyaS81MzcuMzYsR29vZ2xlIEluYy4sLFtvYmplY3QgRGVwcmVjYXRlZFN0b3JhZ2VRdW90YV0sW29iamVjdCBEZXByZWNhdGVkU3RvcmFnZVF1b3RhXSwxMDQwLDAsMCwxOTIwLDI0LDEwODAsW29iamVjdCBTY3JlZW5PcmllbnRhdGlvbl0sMjQsMTkyMCxbb2JqZWN0IERPTVN0cmluZ0xpc3RdLGZ1bmN0aW9uIGFzc2lnbigpIHsgW25hdGl2ZSBjb2RlXSB9LCxtYXRjaC55dWFucmVueHVlLmNvbSxtYXRjaC55dWFucmVueHVlLmNvbSxodHRwOi8vbWF0Y2gueXVhbnJlbnh1ZS5jb20vbWF0Y2gvMTQsaHR0cDovL21hdGNoLnl1YW5yZW54dWUuY29tLC9tYXRjaC8xNCwsaHR0cDosZnVuY3Rpb24gcmVsb2FkKCkgeyBbbmF0aXZlIGNvZGVdIH0sZnVuY3Rpb24gcmVwbGFjZSgpIHsgW25hdGl2ZSBjb2RlXSB9LCxmdW5jdGlvbiB0b1N0cmluZygpIHsgW25hdGl2ZSBjb2RlXSB9LGZ1bmN0aW9uIHZhbHVlT2YoKSB7IFtuYXRpdmUgY29kZV0gfQ=='
    m_exec = get_mjs_exec()
    all_list = []
    for page in range(1, 6):
        # 先请求14m，计算动态参数和提取响应cookie
        url_14m = 'http://match.yuanrenxue.com/api/match/14/m'
        res_14m = req.get(url=url_14m, headers=common_headers)
        js_14m = res_14m.text
        sessionid = res_14m.cookies['sessionid']
        exec_14m = execjs.compile('var window = global;' + js_14m)
        v14 = exec_14m.eval('window["v14"]')
        v142 = exec_14m.eval('window["v142"]')
        # print('提取sessionid：', sessionid)
        # print('计算v14得：', v14)
        # print('计算v142得：', v142)
        # 计算cookie_m
        cookie_m = m_exec.call('get_cookie2', page, v14, v142)
        # print('计算cookie_m得：', cookie_m)
        cookies = {
            'mz': mz,
            'm': cookie_m,
            'sessionid': sessionid
        }
        url_data = 'http://match.yuanrenxue.com/api/match/14?page=' + str(page)
        data_res = req.get(url=url_data, headers=common_headers, cookies=cookies)
        content = data_res.json()
        print(f'第{page}页内容是：{content}')
        all_list.extend([i["value"] for i in content['data']])
    print('总计：', sum(all_list))  # 247986


if __name__ == '__main__':
    request_process()
